package com.know.dao;

import java.util.List;

import my.db.QueryHelper;

import org.apache.commons.lang.StringUtils;

import com.know.entity.Sorts;
import com.kzw.bean.Record;
import com.kzw.dao.support.Page;

public class SortDao {

	/**
	 * 查询分类
	 * @return
	 */
	public List<Sorts> list() {
		String sql="select *from lables";
		return QueryHelper.query(Sorts.class, sql);
	}
	/**
	 * 分页查询
	 */
	public Record findPage(Page<Sorts> page) {
		Record record = new Record();
		String sql = "select * from sorts";
		if(StringUtils.isNotBlank(page.getOrderBy())) {
			sql += String.format(" order by %s %s", page.getOrderBy(), page.getOrder());
		}
		
		List<Sorts> list = QueryHelper.queryPage(Sorts.class, sql, page.getPageNo(), page.getPageSize());

	
		//统计总数count
		String sql2 = sql.toLowerCase().replaceFirst("select .*? from", "select count(*) from");
		long count = QueryHelper.stat(sql2);
		
		record.setCount(count);
		record.setData(list);
		
		return record;
	
	}
	/**
	 * 保存标签
	 * @return 
	 */
	public Record save(Sorts sort) {
		String sql= "insert into sorts values(null,?,?,?,?)";//分类表
		return new Record(false, 0, "暂且不支持增添新分类");
	
	}
	/**
	 * 修改分类
	 */
	public Record update(Sorts sort) {
		Record record=new Record(true);
		String sql="UPDATE sorts SET sort_name=?,  sort_description=? WHERE (sort_id=?)";
		QueryHelper.update(sql, sort.getSort_name(),sort.getSort_description(),sort.getSort_id());
		record.setMsg("成功更新分类");
		return record;
	}
	/**
	 * 根据ID查找
	 */
	public Sorts finById(long parseLong) {
		String sql="select *from sorts where sort_id=?";
		Sorts fl=QueryHelper.queryUnique(Sorts.class, sql, parseLong);
		return fl;
	}
	/**
	 * 删除分类
	 */
	public Record del(long id) {
		String sql="delete from sorts where sort_id =?";
		QueryHelper.update(sql, id);
		String  msg="成功删除";
		return  new Record(true,0,msg);
				
	}
	/**
	 * 批量删除
	 * @param ids
	 */
	public Record multiDel(String ids) {
		String sql = "delete from sorts where sort_id in (" + ids + ")";
		QueryHelper.update(sql);
		String  msg="成功删除";
		return  new Record(true,0,msg);
		
	}
}
